home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5357 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  102 lines

  1. Path: bigblue.oit.unc.edu!mebust
  2. From: mebust@email.unc.edu (Scott Mebust)
  3. Newsgroups: comp.lang.c++
  4. Subject: Sorting pointers to objects with qsort
  5. Date: 4 Feb 1996 03:15:12 GMT
  6. Organization: The University of North Carolina at Chapel Hill
  7. Message-ID: <4f18c0$12do@bigblue.oit.unc.edu>
  8. NNTP-Posting-Host: login0.email.unc.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. I'd appreciate any help on the following problem:  sorting pointers to 
  12. objects (based on object attributes) using qsort.
  13.  
  14. If replying, please send an e-mail as well.  Thanks.
  15.  
  16. Scott
  17. mebust@email.unc.edu
  18.  
  19.  
  20. // QUESTION:  Why does the following code return the wrong results?
  21.  
  22. // I can't seem to figure this one out.  The program below attempts to
  23. // sort an array of pointers to String objects using qsort.  It compiles
  24. // and may *seem* to return the correct results but does not actually
  25. // sort the pointers based on any valid data.
  26.  
  27. // I believe the problem has to do with "qsort" and "const" objects.  It
  28. // appears as though qsort is creating new, const String(s) but the
  29. // str_ member is not copying correctly.
  30.  
  31. // This is probably some *elementary* problem but I just can't *see* what
  32. // is going wrong.
  33.  
  34. // Any help or advice would be greatly appreciated.
  35.  
  36.  
  37. #include <string.h>
  38. #include <iostream.h>
  39. #include <stdlib.h>
  40.  
  41. class String
  42. {
  43.   public:
  44.     String(const char* ccp)
  45.       {  str_=new char[strlen(ccp)+1];  strcpy(str_,ccp); }
  46.     ~String()
  47.       { delete [] str_; }
  48.     String (const String& csr)
  49.       { str_=new char[strlen(csr.str_)+1];  strcpy(str_,csr.str_); }
  50.     String& operator= (const String& csr)
  51.     {
  52.       if (this!=&csr)
  53.       {
  54.         delete[] str_;
  55.         str_=new char[strlen(csr.str_)+1];
  56.         strcpy(str_,csr.str_);
  57.       }
  58.       return *this;
  59.     }
  60.     const char * GetValue(void) const
  61.       { return str_; }
  62.     friend ostream& operator<< (ostream& o, String& s);
  63.  
  64.   private:
  65.     char* str_;
  66. };
  67.  
  68. ostream& operator<< (ostream& o, String& s)
  69. {
  70.   return (o << s.str_);
  71. }
  72.  
  73. int StrCmpFn (const void* VP1, const void* VP2)
  74. {
  75.   return strcmp(((const String*)VP1)->GetValue(),
  76.     ((const String*)VP2)->GetValue());
  77. }
  78.  
  79. void main (void)
  80. {
  81.   String A("Apple"), B("Baseball"), C("Chevrolet");
  82.   String* SPA[3] = {&C, &B, &A};
  83.  
  84.   cout << *SPA[0] << endl
  85.     << *SPA[1] << endl
  86.     << *SPA[2] << endl << endl;
  87.  
  88.   qsort(SPA,3,sizeof(SPA[0]),StrCmpFn);
  89.  
  90.   cout << *SPA[0] << endl
  91.     << *SPA[1] << endl
  92.     << *SPA[2] << endl << endl;
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.